package demo.collection;

import java.util.*;
import java.util.stream.Collectors;

public class Traversal {
    // 集合的遍历
    public static void main(String[] args) {
        /**
         * Collection 接口实现子类
         */
//        traversalList();
//        traversalSet();
        traversalMap();
    }

    // list 集合
    public static void traversalList() {
        List<Integer> list = List.of(3, 5, 4, 9, 6);

        // 1. for 循环遍历
        System.out.println("传统 for循环遍历");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();

        // 2. foreach 循环遍历
        System.out.println("增强 for循环遍历");
        for (Integer i : list) {
            System.out.print(i + " ");
        }
        System.out.println();

        // 3. 迭代器遍历
        System.out.println("迭代器 iterator遍历");
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()){
            Integer i = iterator.next();
            System.out.print(i + " ");
        }
        System.out.println();

        // 4. forEach 方法遍历
        System.out.println("forEach 方法遍历");
        list.forEach(i -> System.out.print(i + " "));
        System.out.println();

        // 5. 使用 stream API 遍历
        System.out.println("stream API 遍历");
        list.stream().forEach(i -> System.out.print(i + " "));
        System.out.println();
    }

    // set 集合
    public static void traversalSet() {
        Set<Integer> set = Set.of(7, 6, 3, 8, 4);
        // 1. foreach 循环遍历
        System.out.println("增强 for循环遍历");
        for (Integer i : set) {
            System.out.print(i + " ");
        }
        System.out.println();

        // 2. 迭代器遍历
        System.out.println("迭代器 iterator遍历");
        Iterator<Integer> iterator = set.iterator();
        while (iterator.hasNext()){
            Integer i = iterator.next();
            System.out.print(i + " ");
        }
        System.out.println();

        // 3. forEach 方法遍历
        System.out.println("forEach 方法遍历");
        set.forEach(i -> System.out.print(i + " "));
        System.out.println();

        // 4. 使用 stream API 遍历
        System.out.println("stream API 遍历");
        set.stream().forEach(i -> System.out.print(i + " "));
        System.out.println();
    }

    // map 集合
    public static void traversalMap(){
        Map<String, Integer> map = Map.of("a", 1, "b",
                2, "c", 3, "d", 4);

        // 1. keySet
        System.out.println("使用 keySet 方法遍历");
        for (String key : map.keySet()) {
            System.out.print(key + "=" + map.get(key) + " ");
        }
        System.out.println();

        // 2. 使用 values 遍历值
        System.out.println("使用 values 方法遍历值");
        for (Integer i : map.values()) {
            System.out.print(i + " ");
        }
        System.out.println();

        // 3. 使用 Map.Entry
        System.out.println("通过 Map.Entry 遍历");
        for (Map.Entry<String, Integer> entry : map.entrySet()){
            System.out.print(entry.getKey() + "=" + entry.getValue() + " ");
        }
        System.out.println();

        // 4. 使用 forEach 方法
        System.out.println("forEach 方法遍历");
        map.forEach((key, value) -> System.out.print(key + "=" + value + " "));
        System.out.println();

        // 5. 使用 Stream API
        System.out.println("stream API 遍历");
        map.entrySet().stream().forEach(
                (entry -> System.out.print(entry.getKey() + "=" + entry.getValue() + " "))
        );
        System.out.println();
    }
}
